package com.example.fileserver_API.utils;

import java.awt.*;
import java.awt.image.BufferedImage;

public class ResizeImageUtils {
    public static BufferedImage resizeImage(BufferedImage originalImage, int targetWidth, int targetHeight) {
        BufferedImage resizedImage = new BufferedImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g = resizedImage.createGraphics();

        // 启用抗锯齿
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);

        int x = (targetWidth - originalImage.getWidth()) / 2;
        int y = (targetHeight - originalImage.getHeight()) / 2;

        g.drawImage(originalImage, x, y, originalImage.getWidth(), originalImage.getHeight(), null);
        g.dispose();

        return resizedImage;
    }
}
